package com.hk.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hk.model.ContactUs;
import com.hk.model.NewsType;
import com.hk.model.vo.ResultVO;
import com.hk.service.INewsService;
import com.hk.service.INewsTypeService;
import com.hk.service.impl.NewsServiceImpl;
import com.hk.service.impl.NewsTypeServiceImpl;
import com.hk.utils.CharacterEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/24 10:28
 * @Statement: 查询所有新闻类型
 */
@WebServlet("/newsType")
public class NewsTypeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        统一字符编码
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        INewsTypeService iNewsTypeService = new NewsTypeServiceImpl();
        List<NewsType> newsTypeList = iNewsTypeService.findAll(-1, -1);
        ResultVO<NewsType> resultVO = new ResultVO<NewsType>();
        PrintWriter out = resp.getWriter();
        if (!newsTypeList.isEmpty()) {
            resultVO.setStatus(1);
            resultVO.setMessage("success");
            resultVO.setTotal(newsTypeList.size());
            resultVO.setList(newsTypeList);
        } else {
            resultVO.setStatus(0);
            resultVO.setMessage("error");
            resultVO.setTotal(newsTypeList.size());
            resultVO.setList(newsTypeList);
        }
        String resultVOJSON = JSONObject.toJSONString(resultVO);
        out.write(resultVOJSON);
        out.flush();
        out.close();

    }
}
